package com.fqgj.sentry.biz.mc;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.sentry.biz.mc.vo.application.ApplicationVo;
import com.fqgj.sentry.biz.mc.vo.menu.MenuVo;
import com.fqgj.sentry.biz.mc.vo.menu.OperatePermissionVo;
import com.fqgj.sentry.biz.mc.vo.menu.ShowMenuVo;
import com.fqgj.sentry.manage.entity.*;
import com.fqgj.sentry.manage.enums.UserLevelEnums;
import com.fqgj.sentry.manage.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by ykpbean kangping.ying@yuntu-inc.com
 *
 * @description
 * @create 2017-07-20 下午3:09
 */

@Component
public class MenuBusiness {
    @Autowired
    private MenuService menuService;
    @Autowired
    private UserService userService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private GroupManagerService groupManagerService;
    @Autowired
    private ApplicationService applicationService;
    @Autowired
    private RolePermissionService rolePermissionService;

    public ShowMenuVo getMenuList(Long userId, Long merchantId) {
        ShowMenuVo showMenuVo=new ShowMenuVo();
        McUserEntity userEntity = userService.selectById(userId);
        if (userEntity.getLevel().equals(UserLevelEnums.merchant_admin.getLevel())) {
            showMenuVo.setAdmin(true);
            showMenuVo.setAccount(userEntity.getAccount());
            getAdminMenu(merchantId,showMenuVo);

        } else if (userEntity.getLevel().equals(UserLevelEnums.nomarl_user.getLevel())) {
            showMenuVo.setAdmin(false);
            showMenuVo.setAccount(userEntity.getAccount());
            getNormalMenu(userId,showMenuVo);
        }
        return showMenuVo;
    }

    private ShowMenuVo getAdminMenu(Long merchantId,ShowMenuVo showMenuVo) {
        //商户系统管理员返回所有菜单 不包含超级管理员菜单
        List<McApplicationEntity> applicationEntityList = applicationService.selectApplicationList(merchantId);
        List<ApplicationVo> applicationVoList = new LinkedList<>();
        HashMap<String, Object> menuMap = new HashMap<>();
        for (McApplicationEntity appEntity : applicationEntityList) {
            ApplicationVo applicationVo = new ApplicationVo()
                    .setAppId(appEntity.getId())
                    .setAppCode(appEntity.getAppCode())
                    .setAppName(appEntity.getAppName());
            applicationVoList.add(applicationVo);
        }
        List<McPermissionEntity> permissionEntityList = permissionService.selectList();
        List<MenuVo> menuVoList = getMenuByPermissionList(permissionEntityList);
        menuMap.put("admin",menuVoList);
        showMenuVo.setApplicationList(applicationVoList);
        showMenuVo.setGroupMenuMap(menuMap);
        return showMenuVo;
    }

    private ShowMenuVo getNormalMenu(Long userId,ShowMenuVo showMenuVo) {
        //商户下的普通用户 先去查询权限组
        List<McGroupManagerEntity> groupManagerEntities = groupManagerService.selectListByUserId(userId);
        List<ApplicationVo> applicationVoList = new LinkedList<>();
        HashMap<String, Object> menuMap = new HashMap<>();
        for (McGroupManagerEntity groupManagerEntity : groupManagerEntities) {
            McApplicationEntity applicationEntity = applicationService.selectByAppId(groupManagerEntity.getAppId());
            if (applicationEntity == null || applicationEntity.getDeleted()) {
                continue;
            }
            ApplicationVo applicationVo = new ApplicationVo()
                    .setAppId(applicationEntity.getId())
                    .setAppCode(applicationEntity.getAppCode())
                    .setAppName(applicationEntity.getAppName());
            applicationVoList.add(applicationVo);
            List<McRolePermissionEntity> rolePermissionEntities = rolePermissionService
                    .selectListByRolesId(groupManagerEntity.getRolesId());
            if (CollectionUtils.isEmpty(rolePermissionEntities)) {
                menuMap.put(applicationVo.getAppCode(), null);
                continue;
            }

            List<McPermissionEntity> permissionEntityList = new ArrayList<>();
            for (McRolePermissionEntity rolePermissionEntity : rolePermissionEntities) {
                McPermissionEntity permissionEntity = permissionService
                        .selectOneById(rolePermissionEntity.getPermissionId());
                if (permissionEntity == null || permissionEntity.getDeleted()) {
                    continue;
                }
                permissionEntityList.add(permissionEntity);
            }
            List<MenuVo> menuVoList = getMenuByPermissionList(permissionEntityList);
            menuMap.put(applicationVo.getAppCode(),menuVoList);
        }
        showMenuVo.setApplicationList(applicationVoList);
        showMenuVo.setGroupMenuMap(menuMap);
        return showMenuVo;
    }

    private List<MenuVo> getMenuByPermissionList(List<McPermissionEntity> permissionEntityList) {
        //菜单
        List<MenuVo> menuVoList = new LinkedList<>();


        List<McMenuEntity> parentMenuList = menuService.findByPid(0L);
        for (McMenuEntity parentMenu : parentMenuList) {
            Boolean parentHasPermission = false;
            MenuVo menuVo = new MenuVo();
            List<McMenuEntity> subMenuList = menuService.findByPid(parentMenu.getId());
            //子菜单
            List<MenuVo> subMenuVoList = new LinkedList<>();
            for (McMenuEntity subMenu : subMenuList) {
                Boolean hasPermission =false;
                MenuVo subMenVo = new MenuVo();
                List<OperatePermissionVo> operatePermissionVoList = new ArrayList<>();
                for (McPermissionEntity permissionEntity : permissionEntityList) {
                    if (permissionEntity.getMenuId().equals(subMenu.getId())) {
                        OperatePermissionVo permissionVo = new OperatePermissionVo()
                                .setPermissionId(permissionEntity.getId())
                                .setPermissionCode(permissionEntity.getPermission())
                                .setDescription(permissionEntity.getDescription())
                                .setMenuId(permissionEntity.getMenuId())
                                .setType(permissionEntity.getType());
                        operatePermissionVoList.add(permissionVo);
                        hasPermission = true;
                        parentHasPermission=true;
                    }
                }
                //如果该子菜单下有相应的操作权限
                if (hasPermission) {
                    subMenVo.setLeaf(true)
                            .setDescription(subMenu.getDescription())
                            .setId(subMenu.getId())
                            .setName(subMenu.getMenu())
                            .setOperatePermission(operatePermissionVoList)
                            .setPid(subMenu.getPid())
                            .setUri(subMenu.getUrl());
                    subMenuVoList.add(subMenVo);
                }
            }
            //如果该子菜单下有相应的操作权限 父菜单也需要显示出来否则不用显示
            if (parentHasPermission) {
                menuVo.setUri(parentMenu.getUrl())
                        .setPid(parentMenu.getPid())
                        .setSubMenu(subMenuVoList)
                        .setLeaf(false)
                        .setName(parentMenu.getMenu())
                        .setId(parentMenu.getId())
                        .setDescription(parentMenu.getDescription());
                menuVoList.add(menuVo);
            }
        }
        return menuVoList;
    }

}
